package com.learn.springboot.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * 验证器配置
 *
 * @author Don
 * @date 2020/07/08
 */
@Configuration
public class ValidatorConfig {

    /**
     * 校验模式 <br/>
     * 1.普通模式（默认是这个模式）: 会校验完所有的属性，然后返回所有的验证失败信息 <br/>
     * 2.快速失败模式: 只要有一个验证失败，则返回 <br/>
     *
     * @return {@link Validator}
     */
    @Bean
    public Validator validator() {
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(true)
                .buildValidatorFactory();
        return validatorFactory.getValidator();
    }
}
